package org.mili.ticketeer.ui.contact;

import org.mili.application.service.ServiceException;
import org.mili.application.service.ServiceFactory;
import org.mili.application.ui.SimpleTableModel;
import org.mili.ticketeer.dto.Contact;
import org.mili.ticketeer.service.ContactService;

import java.util.List;

/**
 */
public class ContactTableModel extends SimpleTableModel<Contact> {
    private ContactService contactService = ServiceFactory.getService(ContactService.class);

    public ContactTableModel() {
        super(new String[]{"No.", "Firstname", "Lastname", "Street", "Number", "ZIP", "Sex", "Phone", "EMail",
                "Token"});
        update();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Contact speaker = get(rowIndex);
        switch(columnIndex) {
            case 0:
                return rowIndex + 1;
            case 1:
                return speaker.getFirstName();
            case 2:
                return speaker.getLastName();
            case 3:
                return speaker.getStreet();
            case 4:
                return speaker.getNumber();
            case 5:
                return speaker.getZip();
            case 6:
                return speaker.getSex();
            case 7:
                return speaker.getPhone();
            case 8:
                return speaker.getEmail();
            case 9:
                return speaker.getToken();
            default: throw new IllegalStateException("no column index for: " + columnIndex);
        }
    }

    @Override
    public List<Contact> load() {
        try {
            return contactService.getContacts();
        } catch (ServiceException e) {
            throw new IllegalStateException(e);
        }
    }

}
